<?php 
	class Timus extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	function get_timu_list($limit = 0, $offset = 0, $conditions = array()){
            $where = " 1 = 1 ";
            if(!empty($conditions['subject_id'])){
                $where .= " AND t.subject_id='{$conditions['subject_id']}' ";
            }
            if(!empty($conditions['chapter_id'])){
                $where .= " AND t.chapter_id='{$conditions['chapter_id']}' ";
            }
            if(!empty($conditions['type'])){
                $where .= " AND t.type='{$conditions['type']}' ";
            }
            if(!empty($conditions['difficulty_level'])){
                $where .= " AND t.difficulty_level='{$conditions['difficulty_level']}' ";
            }

            if(!empty($conditions['subject_text'])){
                $where .= " AND t.subject_text LIKE '%{$conditions['subject_text']}%'";
            }

            if(!empty($conditions['start_time'])){
                $start_time = strtotime($conditions['start_time']);
                $where .= " and t.add_time >= '{$start_time}' ";
            }

            if(!empty($conditions['end_time'])){
                $end_time = strtotime($conditions['end_time']) + 3600 * 24;
                $where .= " and t.add_time <= '{$end_time}' ";
            }

            $sql = "SELECT SQL_CALC_FOUND_ROWS t.*,sub.name as subject_name,book.title as book_name FROM {$this->db->dbprefix}tk_timu_main AS t
            LEFT JOIN {$this->db->dbprefix}b_subject sub on sub.id = t.subject_id
            LEFT JOIN {$this->db->dbprefix}b_book book on book.id = t.book_id 
            WHERE $where ORDER BY id DESC ";
            if($limit > 0 || $offset > 0){
                $sql .= "LIMIT $limit, $offset";
            }
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }

        function get_children_list($limit = 0, $offset = 0, $conditions = array()){
            $where = " 1 = 1 ";
            if(!empty($conditions['timu_id'])){
                $where .= " AND timu_id='{$conditions['timu_id']}' ";
            }

            if(!empty($conditions['type'])){
                $where .= " AND type='{$conditions['type']}' ";
            }

            if(!empty($conditions['title'])){
                $where .= " AND title LIKE '%{$conditions['title']}%'";
            }

            $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}tk_timu_children WHERE $where ORDER BY id DESC ";
            if($limit > 0 || $offset > 0){
                $sql .= "LIMIT $limit, $offset";
            }
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }
	}